
    var websocket = null;
    
    var contextPath = '/ab';
    var basicUrl = window.location.host + contextPath;
    
    var wsUrl = 'ws://' + basicUrl + '/websocket/socketServer.do';  
    var sockUrl = 'http://' + basicUrl + '/socckjs/socketServer.do';  
 
    
    if ('WebSocket' in window) {
    	console.log("WebSocket");
        websocket = new WebSocket(wsUrl);
    } 
    else if ('MozWebSocket' in window) {
    	console.log("MozWebSocket");
        websocket = new MozWebSocket(wsUrl);
    } 
    else {
    	console.log("SockJS");
        websocket = new SockJS(sockUrl);
    }
    websocket.onopen = onOpen;
    websocket.onmessage = onMessage;
    websocket.onerror = onError;
    websocket.onclose = onClose;
              
    function onOpen(event) {
        console.log("socket connected.");
    }
    
    function onMessage(msg) {
    	console.log("Receive msg [" + msg.data + "] from server");
    }
    
    function onError() {
    	console.log("socket connection error.");
    }
    
    function onClose() {
    	console.log("socket connection closed.");    	
    }
    
    function doSend(msg) {
        if (websocket.readyState == websocket.OPEN) {          
            websocket.send(msg);  //后台收到后会调用handleTextMessage方法
            console.log("message: [" + msg + "] sent from client.");  
        } else {  
        	console.log("socket connection failed.");  
        }  
    }
